'use strict';

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const SiteNavigationSchema = new Schema({
    /**
     * 分组
     */
    section: { type: String, default: '' },
    /**
     * 时间
     */
    date: { type: Date },
    /**
     * 标题
     */
    title: { type: String, default: '' },
    /**
     * 链接
     */
    url: { type: String, default: '' },
  });

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取所有类型
   */
  SiteNavigationSchema.statics.findSections = function() {
    return this.distinct('section');
  };

  SiteNavigationSchema.statics.updateModel = function(id, upData) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, upData);
  };

  SiteNavigationSchema.statics.all = function() {
    return this.aggregate()
    .group({ _id: "$section", arr: { $push: "$$ROOT" } })
    .exec()
  };

  /**
   * 删除单个数据
   * @param id 要删除数据的ID
   * @param thoroughly 是否测地删除数据
   * @return {*}
   */
  SiteNavigationSchema.statics.removeModel = function(id) {
    return this.deleteOne({
      _id: mongoose.Types.ObjectId(id),
    });
  };

  return mongoose.model('SiteNavigation', SiteNavigationSchema, 'siteNavigation');
};
